package cn.vihub.utils;

import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T>  implements Serializable {

    // 定义属性
    private Integer code = 200;//错误码

    private String msg = "success";//说明

    private T data;//数据（泛型）

    // 构造器

    // 如果是空的就使用默认值
    public Result() {
    }


    // 自定义属性内容
    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    // 自定义错误码和错误说明
    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    // 自定义返回的对象
    public Result(T data) {
        this.data = data;
    }

    // 静态方法

    public static <T> Result<T> success(){
        return new Result<>();
    }

    public static <T> Result<T> success(T data){
        return new Result<>(data);
    }

    public static <T> Result<T> error(){
        return new Result<>(-1, "error");
    }

    public static <T> Result<T> error(Integer code){
        return new Result<>(code, "error");
    }

    public static <T> Result<T> error(Integer code, String msg){
        return new Result<>(code, msg);
    }

    public static <T> Result<T> error(String msg){
        return new Result<>(-1, msg);
    }

}
